package com.wing.sell.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wing.common.utils.JsonResult;
import com.wing.sell.dao.OrderReviewProcessCompanyDao;
import com.wing.sell.model.entity.OrderReviewProcessCompany;
import com.wing.sell.model.request.OrderReviewProcessCompanyForm;
import com.wing.sell.model.response.OrderReviewProcessCompanyVO;
import com.wing.sell.service.OrderReviewProcessCompanyService;
import com.wing.user.feign.UserClient;
import com.wing.user.model.response.RoleDataVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 订单评审环节公司-ServiceImpl
 *
 * @author
 */
@Service
@Slf4j
public class OrderReviewProcessCompanyServiceImpl extends ServiceImpl<OrderReviewProcessCompanyDao, OrderReviewProcessCompany> implements OrderReviewProcessCompanyService {

    @Autowired
    private UserClient userClient;

    @Override
    public int add(OrderReviewProcessCompanyForm orderReviewProcessCompanyForm) {

        //校验

        OrderReviewProcessCompany orderReviewProcessCompany =new OrderReviewProcessCompany();
        CopyOptions copyOptions = CopyOptions.create()
        .setEditable(OrderReviewProcessCompany.class)
        .setIgnoreError(true)
        .setIgnoreNullValue(true);
        BeanUtil.copyProperties(orderReviewProcessCompanyForm, orderReviewProcessCompany, copyOptions);

        return baseMapper.insert(orderReviewProcessCompany);
    }

    @Override
    public List<OrderReviewProcessCompanyVO> list4app(Map<String, Object> params) {
        return baseMapper.list4app(params);
    }

    @Override
    public Page<OrderReviewProcessCompanyVO> pageList4app(Page page, Map<String, Object> params) {
        List<OrderReviewProcessCompanyVO> list = baseMapper.list4app(page, params);
        page.setRecords(list);
        return page;
    }

    @Override
    public OrderReviewProcessCompanyVO get4appById(Long id) {
        return baseMapper.get4appById(id);
    }

    @Override
    public JsonResult getRoleData(Long processId) {
        List<OrderReviewProcessCompany> orderReviewProcessCompanies = baseMapper.selectList(new QueryWrapper<OrderReviewProcessCompany>().eq("process_id", processId));
        List<Long> ids = new ArrayList<>();
        for (OrderReviewProcessCompany orderReviewProcessCompany : orderReviewProcessCompanies) {
            ids.add(orderReviewProcessCompany.getRoleId());
        }
        if(ids.size() == 0){
            return JsonResult.fail("查询失败");
        }
        JsonResult<List<RoleDataVo>> jsonResult = userClient.getRoleData(ids);
        if(null == jsonResult || jsonResult.getCode() != 200){
            return JsonResult.fail("查询失败");
        }
        if(jsonResult.getData() == null){
            return JsonResult.fail("查询失败");
        }
        return jsonResult;
    }
    
}
